{% extends "base.html" %}
{% block title %}{% endblock %}
{% block content %}
<br>
 <div class="layui-body" style="left:10px">
     <br>
<div class="layui-btn-group" >
    <button class="layui-btn  layui-btn-sm" id="add" type="button">
        <i class="layui-icon">&#xe654;</i>
    </button>
    <button class="layui-btn  layui-btn-sm layui-btn-danger" id="start" type="button">
        <i class="layui-icon">&#xe669;</i>
    </button>
</div>

<table class="layui-hide" id="caseSubPlan" lay-filter="caseSubPlan"></table>
     </div>
<script type="text/html" id="filemange_v1">
    <button type="button" class="layui-btn layui-btn-xs " lay-event="process_add">进度</button>
    <button type="button" class="layui-btn layui-btn-xs layui-btn-danger " lay-event="update">更新</button>
    <button type="button" class="layui-btn  layui-btn-xs" lay-event="delete"><i class="layui-icon">
        &#xe640;</i></button>
</script>
<script>
    layui.use(['table','element'], function(){
          var table = layui.table
          ,form = layui.form;
          var $= layui.jquery;
          var element = layui.element;
          userId = layui.data('test').UserInfo.userId;
          //var loading = layer.load();
          mainPlanCache = layui.data('test').CacheSubFromMain;
          //子任务的增加
          $('#add').on('click',function(){
            layui.data('test', {key: 'CacheSubPlan',value: undefined});
            layui.data('test', {key: 'NewCacheSubPlan',value: {"c_end":mainPlanCache.c_end,"p_end":mainPlanCache.p_end,"project_id":mainPlanCache.project_id,"case_plan_id":mainPlanCache.id}});
            layer.open({
                                                type:2,
                                                content:"/casePlan/sub/manage",
                                                area:['750px','410px'],
                                                title:"子任务管理",
                                                closeBtn:1,
                                                shade: 0,
                                                shadeClose:true,
                                                maxmin: true,
                                                cancel: function(index, layero){
                                                    $('#start').click();
                                                }
            });
          });
          var  options = {
                elem:'#caseSubPlan',
                skin:"line",
                //page:true,
                height: 'full-100',
                toolbar: true,
                url:'/casePlan/sub/srh?',
                limit:100,
                limits:[100],
                where:{
                    case_main_id : mainPlanCache.id,
                }
                ,response:{
                    statusName:'errorCode',
                    statusCode:'',
                    dataName:'data'
                }
                ,cols:[[
                       {type: 'numbers',title:'No.'}
                       ,{field: 'work_type',width:100,sort:true,title: '任务类型', templet: function (d) {
                            if(d.work_type==0){
                                return '用例设计';
                            }else if(d.work_type==1){
                                return '用例执行';
                            }else if(d.work_type==2){
                                return '测试开发';
                            }else if(d.work_type==3){
                                return '测试总结';
                            }else if(d.work_type==4){
                                return '评审相关';
                            }else if(d.work_type==5){
                                return '方案设计';
                            }else if(d.work_type==6){
                                return '其它工作';
                            }
                       }}
                       ,{field: 'work_content',title: '工作内容', templet: function (d) {
                            if(d.work_type==1){
                                return "用例版本："+d.work_content.case_plan+";模块："+d.work_content.case_module;
                            }else{
                                return d.work_content.work_content;
                            }

                       }}
                       ,{field: 'p_end',width:120,title: '计划结束',sort:true}
                       ,{field: 'c_end',width:120,title: '实际结束',sort:true}
                       ,{field: 'id',title: 'id',sort:true,hide:true}
                       ,{field: 'sub_process',width:100, align:'center',title: '进度', templet: function (d) {
                          if(d.taskPrcess != undefined && d.taskPrcess!=d.sub_process){
                            return '<div class="layui-progress layui-progress-big" lay-showPercent="yes">'+
                                    '<div class="layui-progress-bar layui-bg-blue" lay-percent="'+d.taskPrcess+'%"></div></div>';
                          }else{
                            return '<div class="layui-progress layui-progress-big" lay-showPercent="yes">'+
                                    '<div class="layui-progress-bar layui-bg-blue" lay-percent="'+d.sub_process+'%"></div></div>';
                          }
                        }}
                       ,{field: 'person',width:100,title: '责任人',sort:true, templet: function (d) {
                            return d.person_name;
                       }}
                       ,{field: 'taskPrcess',title: 'taskPrcess',sort:true,hide:true, templet: function (d) {
                               if(d.taskPrcess != undefined && d.taskPrcess!=d.sub_process){
                                    data = {"case_plan_id":0,"case_sub_plan_id":d.id,"process":d.taskPrcess,"note":"自动变更","up_person":userId}
                                    $.ajax({
                                                //发出请求
                                                type:"POST",
                                                url:"/casePlan/main/process/manage",
                                                contentType: 'application/json; charset=utf-8',
                                                dataType:"json",
                                                data:JSON.stringify(data),
                                                success: function(result){

                                                }
                                    });

                               }
                       }}
                       ,{fixed: 'right', title:'', toolbar: '#filemange_v1',width:160}
                ]]
                ,id:'caseSubPlan'
                ,done:function(res,cur,count){
                    element.render('sub_process');
                    layui.each(res.data,function(index,item){
                            if(item.sub_process!=100){
                                 if(nowTime() > item.p_end ){
                                    $("tr[data-index='"+index+"']>td[data-field='p_end']").css('color','red');
                                 }
                             }
                    });
                }
          };

          function nowTime(){
                var myDate = new Date;
                var year = myDate.getFullYear(); //获取当前年
                var mon = myDate.getMonth() + 1; //获取当前月
                if(mon<10){
                    mon = "0"+mon;
                }
                var date = myDate.getDate(); //获取当前日
                if(date<10){
                    date = "0"+date;
                }
                return year+"-"+mon+"-"+date;
          }

          var tableIns = table.render(options);
          table.on('row(caseSubPlan)', function(obj){
            obj.tr.css("background","greenyellow");
          });
          //监听表单按钮
          //监听工具条
          table.on('tool(caseSubPlan)', function(obj){
                if(obj.event ==='update'){
                    layui.data('test', {key: 'NewCacheSubPlan',value: undefined});
                    layui.data('test', {key: 'CacheSubPlan',value: {"project_id":mainPlanCache.project_id,"case_plan_id":mainPlanCache.id,"data":obj.data}});
                    layer.open({
                                                        type:2,
                                                        content:"/casePlan/sub/manage",
                                                        area:['750px','410px'],
                                                        title:"子任务管理",
                                                        closeBtn:1,
                                                        shade: 0,
                                                        shadeClose:true,
                                                        maxmin: true,
                                                        cancel: function(index, layero){
                                                            $('#start').click();
                                                        }
                    });
                }else if(obj.event ==='process_add'){
                    if(obj.data.work_type==1){
                        layer.msg("自动获取，无法填写进度");
                        return false;
                    }

                    layui.data('test', {key: 'processList',value: {"id":obj.data.id,"t":1}});
                    layer.open({
                            type:2,
                            content:"/casePlan/main/process",
                            area:['750px','400px'],
                            title:"子任务进度",
                            closeBtn:1,
                            shade: 0,
                            shadeClose:true,
                            maxmin: true,
                            cancel: function(index, layero){
                                $('#start').click();
                            }
                     });

                }else if(obj.event ==='delete'){
                     layer.confirm('真的删除行么?', function(index){

                        $.ajax({
                                    //发出请求
                                    type:"POST",
                                    url:"/casePlan/sub/delete",
                                    contentType: 'application/json; charset=utf-8',
                                    dataType:"json",
                                    data:JSON.stringify({"id":obj.data.id}),
                                    success: function(result){
                                        obj.del();
                                        layer.close(index);
                                        layer.msg("删除成功!");
                                    }
                        });

                     })
                }
          });

          $('#start').on('click',function(){
              options = {
                    elem:'#caseSubPlan',
                    skin:"line",
                    //page:true,
                    height: 'full-100',
                    toolbar: true,
                    url:'/casePlan/sub/srh?',
                    limit:100,
                    limits:[100],
                    where:{
                        case_main_id : mainPlanCache.id,
                    }
                  }
                  tableIns.reload(options);
              });
    });

</script>
{% endblock %}